package com.zhuojy.equity.mall.admin.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuojy.equity.mall.admin.common.entity.TAuditCenter;
import com.zhuojy.equity.mall.admin.common.entity.TPosition;
import com.zhuojy.equity.mall.admin.common.entity.po.THolidayPo;
import com.zhuojy.equity.mall.admin.common.entity.TUser;
import com.zhuojy.equity.mall.admin.common.entity.vo.AuditCenterExportEntity;
import com.zhuojy.equity.mall.admin.common.entity.vo.BalanceExportEntity;
import com.zhuojy.equity.mall.admin.common.entity.vo.BalanceVO;
import com.zhuojy.equity.mall.admin.common.pojo.JsonResult;
import com.zhuojy.equity.mall.admin.common.utils.ExcelUtils;
import com.zhuojy.equity.mall.admin.dal.mapper.AuditCenterMapper;
import com.zhuojy.equity.mall.admin.dal.mapper.THolidayMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Author:LYL
 * @Date 2024/3/19 9:21
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AuditCenterService extends ServiceImpl<AuditCenterMapper, TAuditCenter> {
    private final UserService userService;
    private final THolidayService tHolidayService;
    private final PositionService positionService;
    

    @Autowired
    private THolidayMapper holidayMapper; //假期类型

    /**
     * @description: 审批列表查询 分页 条查
     * @author: lyl
     * @date: 2024/3/19 17:19
     * @param: tAuditCenter
     * @param: pageNum
     * @param: pageSize
     **/
    public JsonResult findTAuditCenterPaginationList(TAuditCenter tAuditCenter, Integer pageNum, Integer pageSize) {
        Page<TAuditCenter> page = new Page<>(pageNum, pageSize);
        IPage<TAuditCenter> tAuditCenterFenYe = this.baseMapper.findTAuditCenterFenYe(tAuditCenter, page);
        return JsonResult.success(tAuditCenterFenYe.getRecords(), tAuditCenterFenYe.getTotal());
    }

    /**
     * @description: 审批列表回显
     * @author: lyl
     * @date: 2024/3/19 17:20
     **/
    public TAuditCenter findTAuditCenterById(Long id) {
        return this.baseMapper.findTAuditCenterById(id);
    }

    /**
     * @description: 根据id 修改状态
     * @author: lyl
     * @date: 2024/3/19 21:03
     * @param: tAuditCenter
     **/
    public void updateStatusById(TAuditCenter tAuditCenter) {

        this.baseMapper.updateStatusById(tAuditCenter);
    }

    /**
     * @description:添加 审批模块
     * @author: lyl
     * @date: 2024/3/19 21:04
     * @param: tAuditCenter
     **/
    public void insertTAuditCenter(TAuditCenter tAuditCenter) {
        tAuditCenter.setSponsorTime(new Date());
        String approver = tAuditCenter.getApprover();
        String[] split = approver.split(",");
        String approverStatus= "";
        for (int i = 0; i < split.length; i++) {
           if(approverStatus.equals("")){
               approverStatus+="1";
           }else {
               approverStatus+=",1";
           }
        }
        tAuditCenter.setApprovalStatus(approverStatus);
        this.baseMapper.insert(tAuditCenter);
    }
    //已审核

    public JsonResult findTAuditCenterPaginationList2(TAuditCenter tAuditCenter, Integer pageNum, Integer pageSize) {
        Page<TAuditCenter> page = new Page<>(pageNum, pageSize);
        IPage<TAuditCenter> tAuditCenterFenYe2 = this.baseMapper.findTAuditCenterFenYe2(tAuditCenter, page);
        return JsonResult.success(tAuditCenterFenYe2.getRecords(), tAuditCenterFenYe2.getTotal());

    }

    //我发起的
    public JsonResult findTAuditCenterPaginationList4(TAuditCenter tAuditCenter, Integer pageNum, Integer pageSize) {
        Page<TAuditCenter> page = new Page<>(pageNum, pageSize);
        IPage<TAuditCenter> tAuditCenterFenYe4 = this.baseMapper.findTAuditCenterFenYe4(tAuditCenter, page);
        return JsonResult.success(tAuditCenterFenYe4.getRecords(), tAuditCenterFenYe4.getTotal());
    }

    //我收到的
    public JsonResult findTAuditCenterPaginationList3(TAuditCenter tAuditCenter, Integer pageNum, Integer pageSize) {
        Page<TAuditCenter> page = new Page<>(pageNum, pageSize);
        IPage<TAuditCenter> tAuditCenterFenYe3 = this.baseMapper.findTAuditCenterFenYe3(tAuditCenter, page);
        return JsonResult.success(tAuditCenterFenYe3.getRecords(), tAuditCenterFenYe3.getTotal());
    }

    //请假记录表条查分页
    public JsonResult findTAuditPagination(AuditCenterExportEntity auditCenterExportEntity, Integer pageNum, Integer pageSize) {
        Page<TAuditCenter> page = new Page<>(pageNum, pageSize);
        IPage<TAuditCenter> auditCenterExportEntityIPage = this.baseMapper.findTAuditPagination(auditCenterExportEntity, page);
        return JsonResult.success(auditCenterExportEntityIPage.getRecords(), auditCenterExportEntityIPage.getTotal());
    }

    //请假记录表删除
    public void deleteTAuditByIds(Integer id) {
        this.baseMapper.deleteById(id);
    }

    //请假记录表导出
    public void exportExcel(HttpServletResponse response) {
        List<AuditCenterExportEntity> list = this.baseMapper.getAuditCenterExpoet();
        ExcelUtils.export(response, "导出数据列表", list, AuditCenterExportEntity.class);
    }

    //请假记录表条查动态下拉列表
    public List<TAuditCenter> findOptions() {
        return this.baseMapper.findOptions();
    }

    //员工假期余额表条查分页
    public JsonResult findbalancePagination( TAuditCenter tAuditCenter, Integer pageNum, Integer pageSize) {
        Page<TAuditCenter> page = new Page<>(pageNum, pageSize);
        IPage<TAuditCenter> tAuditCenterIPage = this.baseMapper.findbalancePagination(tAuditCenter, page);
        return JsonResult.success(tAuditCenterIPage.getRecords(), tAuditCenterIPage.getTotal());
    }

    //员工余额表多表导出
    public void exportblanceExcel(HttpServletResponse response) {
        List<BalanceExportEntity> list = this.baseMapper.getBalanceExportEntity();
        ExcelUtils.export(response, "导出数据列表", list, BalanceExportEntity.class);
    }

    //员工余额表回显
    public BalanceVO findBalanceById(Integer id) {
        TAuditCenter tAuditCenter = this.baseMapper.selectById(id);
        TUser tUser = userService.getById(tAuditCenter.getSponsorId());
        TPosition tPosition = positionService.getById(tAuditCenter.getDeptId());
        THolidayPo tHoliday = tHolidayService.getById(tAuditCenter.getLeaveType());
        BalanceVO balanceVO = new BalanceVO();
        BeanUtils.copyProperties(tAuditCenter,balanceVO);
        BeanUtils.copyProperties(tUser,balanceVO);
        BeanUtils.copyProperties(tPosition,balanceVO);
        BeanUtils.copyProperties(tHoliday,balanceVO);
        System.out.println(balanceVO);
        return balanceVO;

    }
    //员工余额表修改
    public void updateBalance(BalanceVO balanceVO) {
        TAuditCenter tAuditCenter = new TAuditCenter();
        TUser tUser = new TUser();
        TPosition tPosition = new TPosition();
        THolidayPo tHoliday = new THolidayPo();

        BeanUtils.copyProperties(balanceVO,tAuditCenter);

        BeanUtils.copyProperties(balanceVO,tPosition);
        tPosition.setId(tAuditCenter.getDeptId().longValue());

        BeanUtils.copyProperties(balanceVO,tUser);
        tUser.setId(tAuditCenter.getSponsorId().longValue());

        BeanUtils.copyProperties(balanceVO,tHoliday);
        tHoliday.setId(tAuditCenter.getLeaveType().longValue());

        updateById(tAuditCenter);
        userService.updateById(tUser);
        positionService.updateById(tPosition);
        tHolidayService.updateById(tHoliday);


    }

    /**
     * @description:审批列表导出
     * @author: lyl
     * @date: 2024/4/2 19:40
     * @param: response
     **/
    public void getAuditCenterEntity(HttpServletResponse response) {
        List<TAuditCenter> auditCenterEntity = this.baseMapper.getAuditCenterEntity();
        ExcelUtils.export(response, "导出审批列表", auditCenterEntity, TAuditCenter.class);
    }

    public void updateApprovalStatusById(TAuditCenter tAuditCenter) {
        String approvalStatus = tAuditCenter.getApprovalStatus();
        String[] start =approvalStatus.split(",");
        boolean flag=false;
        boolean flag2=false;
        for (int i = 0; i < start.length; i++) {
            if(start[i].equals("2")){
                flag=true;
            }else{
                flag=false;

            }
            if(start[i].equals("3")){
                flag2=true;
                break;
            }

        }
        if( flag){
            tAuditCenter.setStatus(2);
        }
        if(flag2){
            tAuditCenter.setStatus(3);
        }
        this.baseMapper.updateById(tAuditCenter);

    }
}
